<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>声明周期</title>
</head>
<body>
    <button>来个看图神器</button>
    <button>下去吧</button>
    <hr>

    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        //
        class App extends React.Component{
            state = {
                url: 'http://api.xiaohigh.com/image/random'
            }

            render(){
                return <img width="500" src={this.state.url} alt=""  onClick={this.change} />
            }

            change = () => {
                this.setState({
                    url: 'http://api.xiaohigh.com/image/random?t=' + Date.now()
                })
            }

            //生命周期回调   可选的
            componentDidMount(){
                console.log('组件挂载完毕了....');
            }

            //将要卸载的回调
            componentWillUnmount(){
                console.log('组件将要卸载了...');
            }

            //组件更新
            componentDidUpdate(){
                console.log('组件完成了更新...');
            }
            

        }

        //获取两个按钮
        let btns = document.querySelectorAll('button');

        //第一个按钮单击事件
        btns[0].onclick = function(){
            //挂载 渲染组件
            ReactDOM.render(<App />, document.querySelector("#root"));
        }

        //第二个按钮单击事件
        btns[1].onclick = function(){
            //卸载组件  unmount 卸载  component 组件  at 在   node 节点
            ReactDOM.unmountComponentAtNode(document.querySelector("#root"));
        }
    </script>
</body>
</html>